<template>
<span>
  <span v-if="!outTime">
    {{m}}分{{s}}秒
  </span>
  <span v-else>
    已过期
  </span>
</span>
</template>

<script>
import {leftTime} from '../common/js/util'
import {commonAjax, PLATFORM_PREFIX} from '../api/api'
export default {
  name: 'time-reduce',
  props: ['displayTime'],
  data () {
    return {
      m: 0,
      s: 0,
      outTime: false
    }
  },
  mounted () {
    const ORDER_OUT_DATE_TIME = 0
    let _this = this
    if (!this.displayTime) {
      this.outTime = true
    }
    let serviceName = ''
    if (PLATFORM_PREFIX === 'cas') {
      serviceName = 'appService'
    } else {
      serviceName = 'wechatService'
    }
    commonAjax([], `${PLATFORM_PREFIX}.${serviceName}`, 'getServerTime').then(res => {
      if (res.data.code === 200) {
        let orderCreateTime = new Date(this.displayTime.replace(/-/g, '/')).getTime()
        let dist = new Date(res.data.body.serverTime.replace(/-/g, '/')) - new Date()
        let timer = setInterval(() => {
          var left = leftTime(ORDER_OUT_DATE_TIME, (dist + (new Date() - orderCreateTime)))
          if (left.m <= 0 && left.s <= 0) {
            clearInterval(timer)
            _this.outTime = true
            location.reload()
          } else {
            _this.m = left.m
            _this.s = left.s
          }
        }, 1000)
      }
    })
  }
}
</script>

